package seqlist.oj;

public class Num142 {
    public ListNode detectCycle(ListNode head) {
        if(head==null || head.next==null){
            return null;
        }
        ListNode low=head,fast=head;
        while(fast!=null && fast.next!=null){
            low=low.next;
            fast=fast.next.next;
            if(low==fast){
                ListNode third=head;
                while (third!=low){
                    low=low.next;
                    third=third.next;
                }
                return low;
            }
        }
        return null;
    }
}
